package com.example.peibei.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListPopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.dingtao.common.bean.RecommendMember;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDActivity;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.SPUtils;
import com.dingtao.common.util.UIUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.service.presenter.CateOrderPresenter;
import com.example.peibei.service.presenter.RecommendMemberMorePresenter;
import com.example.peibei.ui.adapter.MoreRecommendListAdapter;
import com.example.peibei.ui.adapter.RecommendMemberAdapter;
import com.example.peibei.ui.weight.EmptyRecyclerView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MoreRecommendActivity extends WDActivity {
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.rv_order_list)
    EmptyRecyclerView orderList;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.ll_one)
    LinearLayout ll_one;
    @BindView(R.id.ll_two)
    LinearLayout ll_two;
    @BindView(R.id.empty_view)
    LinearLayout empty_view;
    @BindView(R.id.tv_cate_one)
    TextView tv_cate_one;
    @BindView(R.id.tv_cate_two)
    TextView tv_cate_two;
    @BindView(R.id.tv_cate_three)
    TextView tv_cate_three;

    private String price = "";
    private String type = "";
    private int age = 0;
    private String auto = "";
    private int categoryId;
    private String caption;
    private String gender = "";
    private MoreRecommendListAdapter homeOrderListAdapter;
    private CateOrderPresenter homeOrderPresenter;
    List<RecommendMember.RecordsDTO> orders;
    private SPUtils spUtils;
    private String cityName;
    private String cityCode;
    private RecommendMemberAdapter recommendMemberAdapter;
    private RecommendMemberMorePresenter recommendMemberPresenter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_cate_order;
    }

    @Override
    protected void initView() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
//        caption = bundle.getString("caption");
//        categoryId = bundle.getInt("id");
        tv_title.setText("超值服务");
        spUtils = new SPUtils(this, SpConstant.USER_TOKEN);
//        cityName = spUtils.getString(SpConstant.CITY_NAME);
//        cityCode = spUtils.getString(SpConstant.CITY_CODE);
        orders = new ArrayList<>();
//        orderList.setLayoutManager(new LinearLayoutManager(this));
        orderList.setLayoutManager(new LinearLayoutManager(this));
        homeOrderListAdapter = new MoreRecommendListAdapter(orders);
//        orderList.setAdapter(homeOrderListAdapter);
//        homeOrderPresenter = new CateOrderPresenter(new OrderCall());
//        recommendMemberAdapter = new RecommendMemberAdapter(orders);
        orderList.setAdapter(homeOrderListAdapter);
        orderList.setEmptyView(empty_view);
        recommendMemberPresenter = new RecommendMemberMorePresenter(new RecommendMemberCall());

        homeOrderListAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                List<RecommendMember.RecordsDTO> data = (List<RecommendMember.RecordsDTO>) adapter.getData();
                RecommendMember.RecordsDTO recordsDTO = data.get(position);
                String publishOrderId = recordsDTO.getPublishOrderId();
                Bundle bundle = new Bundle();
                bundle.putString("publishOrderId", publishOrderId);
                intent(OrderDetailActivity.class, bundle);
            }
        });

        onRefreshStart();
        refreshLayout.setOnRefreshListener(refreshLayout -> onRefreshStart());
        homeOrderListAdapter.setOnItemClickListener((adapter, view, position) -> {
            RecommendMember.RecordsDTO order = (RecommendMember.RecordsDTO) adapter.getData().get(position);
            String publishOrderId = order.getPublishOrderId();
            Bundle bundle1 = new Bundle();
            bundle1.putString("publishOrderId",publishOrderId);
            intent(OrderDetailActivity.class, bundle1);
        });
    }

    public void onRefreshStart() {
        if (recommendMemberPresenter.isRunning()) {
            refreshLayout.finishRefresh(true);
            return;
        }
        recommendMemberPresenter.reqeust(true,auto,gender,price);
//        homeOrderPresenter.reqeust(true, categoryId + "", type, age, auto, cityCode, cityName, gender, price);
    }

    @OnClick(R.id.iv_back)
    public void back() {
        finish();
    }

    @OnClick(R.id.ll_one)
    public void chooseOne() {
        boolean isVip = spUtils.getBoolean(SpConstant.VIP, false);
        if (!isVip){
            intent(MemberActivity.class);
            return;
        }
        PopupWindowAdapter popupWindowAdapter = new PopupWindowAdapter(this, getDataOne());
        ListPopupWindow listPopupWindow = new ListPopupWindow(this);
        listPopupWindow.setWidth(getResources().getDisplayMetrics().widthPixels);
        listPopupWindow.setAnchorView(ll_one);
        listPopupWindow.setAdapter(popupWindowAdapter);
//        ListView listView = listPopupWindow.getListView();
//        listView.setChoiceMode(CHOICE_MODE_SINGLE);
        listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                UIUtils.showToastSafe(list.get(position));
                tv_cate_one.setText(list.get(position));
                auto = position + "";
                onRefreshStart();
                listPopupWindow.dismiss();
            }
        });
        if (!listPopupWindow.isShowing()) {
            listPopupWindow.show();
        }
    }

    ArrayAdapter<String> adapter;
    ArrayList<String> list = new ArrayList<String>();

    private ArrayList<String> getDataOne() {
        list.clear();
        list.add("智能排序");
        list.add("最新发布");
        list.add("距离最近");
        return list;
    }

    private ArrayList<String> getDataTwo() {
        list.clear();
        list.add("只看女");
        list.add("只看男");
        list.add("不限");
        return list;
    }

    private ArrayList<String> getDataThree() {
        list.clear();
        list.add("价格降序");
        list.add("价格升序");
        return list;
    }

    @OnClick(R.id.ll_two)
    public void chooseTwo() {
        boolean isVip = spUtils.getBoolean(SpConstant.VIP, false);
        if (!isVip){
            intent(MemberActivity.class);
            return;
        }
        PopupWindowAdapter popupWindowAdapter = new PopupWindowAdapter(this, getDataTwo());
        ListPopupWindow listPopupWindow = new ListPopupWindow(this);
        listPopupWindow.setWidth(getResources().getDisplayMetrics().widthPixels);
        listPopupWindow.setAnchorView(ll_two);
        listPopupWindow.setAdapter(popupWindowAdapter);
        listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                tv_cate_two.setText(list.get(position));
                if (position == 2) {
                    gender = "";
                } else {
                    gender = position + "";
                }
                onRefreshStart();
                listPopupWindow.dismiss();
            }
        });
        if (!listPopupWindow.isShowing()) {
            listPopupWindow.show();
        }
    }

    @OnClick(R.id.ll_three)
    public void chooseThree() {
        boolean isVip = spUtils.getBoolean(SpConstant.VIP, false);
        if (!isVip){
            intent(MemberActivity.class);
            return;
        }
        PopupWindowAdapter popupWindowAdapter = new PopupWindowAdapter(this, getDataThree());
        ListPopupWindow listPopupWindow = new ListPopupWindow(this);
        listPopupWindow.setWidth(getResources().getDisplayMetrics().widthPixels);
        listPopupWindow.setAnchorView(ll_two);
        listPopupWindow.setAdapter(popupWindowAdapter);
        listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                UIUtils.showToastSafe(list.get(position));
                tv_cate_three.setText(list.get(position));
                price = position + 1 + "";
                onRefreshStart();
                listPopupWindow.dismiss();
            }
        });
        if (!listPopupWindow.isShowing()) {
            listPopupWindow.show();
        }
    }

    class RecommendMemberCall implements DataCall<RecommendMember> {

        @Override
        public void success(RecommendMember data, Object... args) {
            //添加列表并刷新
            if (recommendMemberPresenter.getPage() == 1) {
                orders.clear();
            }
            orders.addAll(data.getRecords());
            homeOrderListAdapter.notifyDataSetChanged();
        }

        @Override
        public void fail(ApiException e, Object... args) {
            refreshLayout.finishRefresh();
            refreshLayout.finishLoadMore();
        }
    }
//
//    class OrderCall implements DataCall<Order> {
//
//        @Override
//        public void success(Order data, Object... args) {
//            refreshLayout.finishRefresh(true);
//            //添加列表并刷新
//            if (homeOrderPresenter.getPage() == 1) {
//                orders.clear();
//            }
//            homeOrderListAdapter.setNewInstance(data.getRecords());
//            homeOrderListAdapter.notifyDataSetChanged();
//        }
//
//        @Override
//        public void fail(ApiException e, Object... args) {
//            refreshLayout.finishRefresh(true);
//        }
//    }

    public class PopupWindowAdapter extends BaseAdapter {
        private ArrayList<String> datas;
        private Context context;

        public PopupWindowAdapter(Context context, ArrayList<String> datas) {
            this.context = context;
            this.datas = datas;
        }

        public int getCount() {
            return datas.size();
        }

        public Object getItem(int position) {
            return datas.get(position);
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.item_pupwindow, null);
                TextView tv = (TextView) convertView.findViewById(R.id.tv_name);
                tv.setText(datas.get(position));
            }
            return convertView;
        }


    }

    @Override
    protected void destoryData() {

    }
}
